﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CoolCode;

namespace CoolCode.Entities
{
    public class InspectionComparer : IEqualityComparer<Inspection>
    {
        #region IEqualityComparer<Inspection> Members

        public bool Equals(Inspection x, Inspection y)
        {
            return x.ProductID == y.ProductID;
        }

        public int GetHashCode(Inspection obj)
        {
            return obj.ProductID.GetHashCode();
        }

        #endregion
    }

    /// <summary>
    /// 检查
    /// </summary>
    public partial class Inspection : ITrackEntity
    {
        
        /// <summary>
        /// 胶面外径(Highlight)
        /// </summary>
        public string OutsideDiameterHighlight
        {
            get
            {
                return string.Format("{0}/{1}", Highlight(PassODFar, ODFar ), Highlight(PassODClose, ODClose));
            }
        }
        
        /// <summary>
        /// 220KPa压力测试（外径/长度）
        /// </summary>
        public string StressTestHighlight
        {
            get
            {
                return string.Format("{0}/{1}//{2}", ODStressTest1, Highlight( PassODStressTest2, ODStressTest2),
                   Highlight(PassStressTestLength, StressTestLength, "0.##"));
            }
        }

        /// <summary>
        /// 胶面外径
        /// </summary>
        public string OutsideDiameter
        {
            get
            {
                return string.Format("{0:0.00}/{1:0.00}", ODFar, ODClose);
            }
            set
            {
                if (!string.IsNullOrEmpty(value))
                {
                    string [] splits =value.Split('/');
                    if (splits.Length >= 2)
                    {
                        decimal tempNum;
                        if (decimal.TryParse(splits[0], out tempNum))
                        {
                            this.ODFar = tempNum;
                        }
                        if (decimal.TryParse(splits[1], out tempNum))
                        {
                            this.ODClose = tempNum;
                        }
                    }
                }
            }
        }

        /// <summary>
        /// 220KPa压力测试（外径/长度）
        /// </summary>
        public string StressTest
        {
            get
            {
                return string.Format("{0:0.00}/{1:0.00}//{2:0.##}", ODStressTest1, ODStressTest2, StressTestLength);
            }
            set
            {
                if (!string.IsNullOrEmpty(value))
                {
                    string[] splits = value.Split('/');
                    if (splits.Length >= 3)
                    {
                        decimal tempNum;
                        if (decimal.TryParse(splits[0], out tempNum))
                        {
                            this.ODStressTest1 = tempNum;
                        }
                        if (decimal.TryParse(splits[1], out tempNum))
                        {
                            this.ODStressTest2 = tempNum;
                        }
                        if (decimal.TryParse(splits[2], out tempNum))
                        {
                            this.StressTestLength  = tempNum;
                        }
                        if (splits.Length >3 && decimal.TryParse(splits[3], out tempNum))
                        {
                            this.StressTestLength = tempNum;
                        }
                    }
                }
            }
        }

        /// <summary>
        /// 远端外径标准通过
        /// </summary>
        public bool PassODFar
        {
            get;
            set;
            //get
            //{
            //    SpecType specType = this.GetSpecType();
            //    switch (specType)
            //    {
            //        case SpecType._22:
            //        case SpecType._24:
            //        case SpecType._26:
            //            return ODFar.InRange(3.8m, 4.0m);
            //        case SpecType._28:
            //        case SpecType._30:
            //            return ODFar.InRange(3.8m, 4.1m);
            //    }
            //    return false;
            //}
        }

        /// <summary>
        /// 近端外径标准通过
        /// </summary>
        public bool PassODClose
        {
            get;
            set;
            //get
            //{
            //SpecType specType = this.GetSpecType();
            //switch (specType)
            //{
            //    case SpecType._22:
            //    case SpecType._24:
            //    case SpecType._26:
            //        return ODClose.InRange(3.8m, 4.2m);
            //    case SpecType._28:
            //    case SpecType._30:
            //        return ODClose.InRange(3.8m, 4.3m);
            //}
            //return false;
            //}

        }

        /// <summary>
        /// 椎体高度标准通过
        /// </summary>
        public bool PassConeHeight
        {
            get;
            set;
            //get
            //{
            //    SpecType specType = this.GetSpecType();
            //    switch (specType)
            //    {
            //        case SpecType._22:
            //        case SpecType._24:
            //        case SpecType._26:
            //            return ConeHeight.InRange(6.00m, 6.70m);
            //        case SpecType._28:
            //        case SpecType._30:
            //            return ConeHeight.InRange(6.10m, 6.70m);
            //    }
            //    return false;
            //}
        }

        /// <summary>
        /// 球囊自然长度标准通过
        /// </summary>
        public bool PassSacculusLength
        {
            get;
            set;
            //get
            //{
            //    SpecType specType = this.GetSpecType();
            //    switch (specType)
            //    {
            //        case SpecType._22:
            //        case SpecType._24:
            //        case SpecType._26:
            //            return  SacculusLength <29.00m ;
            //        case SpecType._28:
            //        case SpecType._30:
            //            return SacculusLength < 30.00m;
            //    }
            //    return false;
            //}
        }

        /// <summary>
        /// 球囊伸长长度标准通过
        /// </summary>
        public bool PassSacculusStretch
        {
            get;
            set;
        }

        /// <summary>
        /// 220kPa 外径标准通过
        /// </summary>
        public bool PassODStressTest2
        {
            get;
            set;
            //get
            //{
            //    SpecType specType = this.GetSpecType();
            //    switch (specType)
            //    {
            //        case SpecType._22:
            //            return ODStressTest2.InRange(20.50m, 21.80m);
            //        case SpecType._24:
            //            return ODStressTest2.InRange(22.50m, 23.80m);
            //        case SpecType._26:
            //            return ODStressTest2.InRange(24.50m, 25.80m);
            //        case SpecType._28:
            //            return ODStressTest2.InRange(26.50m, 27.80m);
            //        case SpecType._30:
            //            return ODStressTest2.InRange(28.50m, 29.80m);
            //    }
            //    return false;
            //}
        }


        /// <summary>
        /// 220kPa 充盈球囊长度标准通过
        /// </summary>
        public bool PassStressTestLength
        {
            get;
            set;
            //get
            //{
            //    SpecType specType = this.GetSpecType();
            //    switch (specType)
            //    {
            //        case SpecType._22:
            //            return StressTestLength.InRange(35.00m, 37.00m);
            //        case SpecType._24:
            //            return StressTestLength.InRange(37.00m, 39.00m);
            //        case SpecType._26:
            //            return StressTestLength.InRange(39.00m, 41.00m);
            //        case SpecType._28:
            //            return StressTestLength.InRange(40.00m, 42.00m);
            //        case SpecType._30:
            //            return StressTestLength.InRange(41.10m, 43.00m);
            //    }
            //    return false;
            //}
        }

        private string Highlight(bool pass, decimal d )
        {
           return   Highlight(pass, d, "0.00");
        }

        private string Highlight(bool pass, decimal d, string format)
        {
            return pass ? string.Format("{0:" + format + "}", d) : string.Format("<span class='notPass'>{0:" + format + "}</span>", d);
        }

        private SpecType GetSpecType()
        {
            return Enum<SpecType>.Find(c => c.GetAttachedString() == this.ProductSpec);
        }
 
    }

    public enum SpecType
    {
        [AttachString("")]
        All,
        [AttachString("22")]
        _22,
        [AttachString("24")]
        _24,
        [AttachString("26")]
        _26,
        [AttachString("28")]
        _28,
        [AttachString("30")]
        _30,
    }

}
